/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.model.provider;

import java.util.List;

import com.siro.pm.PmApplication;
import com.siro.pm.model.Developer;
import com.siro.pm.model.Project;
import com.vaadin.data.Item;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.terminal.ThemeResource;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since MenuTreeProvider.java v1.0
 */
public class MenuTreeProvider {
	public static String PROJECT_PROPERTY_PROJECT = "project";
	public static String PROJECT_PROPERTY_NAME = "name";
	public static String PROJECT_PROPERTY_ICON = "icon";

	static HierarchicalContainer menuTreeContainer;
	static {
		menuTreeContainer = new HierarchicalContainer();
		menuTreeContainer.addContainerProperty(PROJECT_PROPERTY_NAME,
				String.class, null);
		menuTreeContainer.addContainerProperty(PROJECT_PROPERTY_ICON,
				ThemeResource.class, new ThemeResource(
						"../siropm/img/folder.gif"));
		menuTreeContainer.addContainerProperty(PROJECT_PROPERTY_PROJECT,
				Project.class, null);
	}

	public static HierarchicalContainer getTreeMenuContainer() {
		menuTreeContainer.removeAllItems();
		List<Project> projects = ProjectProvider
				.getProjectsData((Developer) PmApplication.getDefault()
						.getUser());
		for (Project project : projects) {
			Item item_project = menuTreeContainer.addItem(project);
			item_project.getItemProperty(PROJECT_PROPERTY_PROJECT).setValue(
					project);
			// Add name property for item
			item_project.getItemProperty(PROJECT_PROPERTY_NAME).setValue(
					project.getName());
			// Allow children
			menuTreeContainer.setChildrenAllowed(project, true);

			// Add child items
			{
				if (project.getDevelopers() != null) {
					String item_id = project.getId()
							+ ProjectProvider.PROJECT_PROPERTY_DEVELOPERS;
					Item item_Developers = menuTreeContainer.addItem(item_id);
					item_Developers.getItemProperty(PROJECT_PROPERTY_PROJECT)
							.setValue(project);
					item_Developers
							.getItemProperty(PROJECT_PROPERTY_NAME)
							.setValue(
									ProjectProvider.PROJECT_PROPERTY_DEVELOPERS);
					item_Developers
							.getItemProperty(PROJECT_PROPERTY_ICON)
							.setValue(
									new ThemeResource(
											"../siropm/img/project_developer.png"));
					menuTreeContainer.setParent(item_id, project);
					menuTreeContainer.setChildrenAllowed(item_id, false);
				}

				if (project.getDocs() != null) {
					String item_id = project.getId()
							+ ProjectProvider.PROJECT_PROPERTY_DOCUMENTS;
					Item item_Documents = menuTreeContainer.addItem(item_id);
					item_Documents.getItemProperty(PROJECT_PROPERTY_PROJECT)
							.setValue(project);
					item_Documents.getItemProperty(PROJECT_PROPERTY_NAME)
							.setValue(
									ProjectProvider.PROJECT_PROPERTY_DOCUMENTS);
					item_Documents
							.getItemProperty(PROJECT_PROPERTY_ICON)
							.setValue(
									new ThemeResource(
											"../siropm/img/project_documents.png"));
					menuTreeContainer.setParent(item_id, project);
					menuTreeContainer.setChildrenAllowed(item_id, false);
				}

				if (project.getDemandsOrBugs() != null) {
					String item_id = project.getId()
							+ ProjectProvider.PROJECT_PROPERTY_BUGS;
					Item item_Bugs = menuTreeContainer.addItem(item_id);
					item_Bugs.getItemProperty(PROJECT_PROPERTY_PROJECT)
							.setValue(project);
					item_Bugs.getItemProperty(PROJECT_PROPERTY_NAME).setValue(
							ProjectProvider.PROJECT_PROPERTY_BUGS);
					item_Bugs.getItemProperty(PROJECT_PROPERTY_ICON)
							.setValue(
									new ThemeResource(
											"../siropm/img/project_bugs.gif"));
					menuTreeContainer.setParent(item_id, project);
					menuTreeContainer.setChildrenAllowed(item_id, false);
				}
			}
		}
		return menuTreeContainer;
	}

}
